#coding=utf-8
#!/usr/bin/python

import os, subprocess,sys
import random

# use subprocess to get the current branch name from output
def get_branch_name(cd_path):
    os.chdir(cd_path)
    status, branch_name = subprocess.getstatusoutput("git branch | sed -n '/\* /s///p'")
    # print(output)
    # exit(0)
    return branch_name

def get_latest_git_log(cd_path):
    """
    docstring
    """
    os.chdir(cd_path)
    status, log_info = subprocess.getstatusoutput("git log --pretty=format:\"%s\" -1")
    return log_info

def get_latest_commit_id(cd_path):
    os.chdir(cd_path)
    status, commit_id = subprocess.getstatusoutput("git rev-parse HEAD")
    return commit_id

def get_reviewer_by_random(reviewers):
    return random.choice(reviewers)

def get_thanks_words_by_random(thanks_words):
    return random.choice(thanks_words)

def create_comment(reviewers, branch_name, log_info, commit_id, thanks_words, git_commit_uri):
    print(get_reviewer_by_random(reviewers))
    print("*Changes made has been committed to " + branch_name + "*")
    # www.davesgarden.com
    print("- " + git_commit_uri + commit_id)
    print("*Details*")
    print("-" + log_info)
    print(get_thanks_words_by_random(thanks_words))


argvs = sys.argv


target_pathes = [
    '/Users/tony/www/www.davesgarden.com', 
    '/Users/tony/www/Davesgarden', 
    '/Users/tony/www/gallery-core', 
    '/Users/tony/www/newworkspace/www.weddingbee.com/htdocs',
    '/Users/tony/www/qbproject/htdocs',
    '/Users/tony/dockerFiles/fodors-newsletter-manager/fodors-docker-container/sites/fodors-newsletter-manager',
    '/Users/tony/dockerFiles/flyertalk/flyertalk-docker-container/sites/flyertalk-trending-hp',
    '/Users/tony/dockerFiles/fodors-newsletter-manager/fodors-docker-container/sites/fodors.com',
    '/Users/tony/dockerFiles/fodors-newsletter-manager/fodors-docker-container/sites/fodors.com-wordpress',
    '/Users/tony/dockerFiles/flyertalk/flyertalk-docker-container/sites/travel-newsletter-manager',
    '/Users/tony/dockerFiles/flyertalk/flyertalk-docker-container/sites/flyertalk-wordpress'

]
git_commit_log_uris = [
    'https://git.internetbrands.com/content-templates/www.davesgarden.com/-/commit/',
    'https://git.internetbrands.com/Home-Communities/Davesgarden/-/commit/',
    'https://git.internetbrands.com/content/gallery-core/-/commit/',
    'https://git.internetbrands.com/Home-Communities/Weddingbee/-/commit/',
    'https://git.internetbrands.com/Home-Communities/Quiltingboard/-/commit/',
    'https://git.internetbrands.com/travel/fodors-newsletter-manager/-/commit/',
    'https://git.internetbrands.com/travel/flyertalk-trending-hp/-/commit/',
    'https://git.internetbrands.com/travel/fodors.com/-/commit/',
    'https://git.internetbrands.com/travel/fodors.com-wordpress/-/commit/',
    'https://git.internetbrands.com/travel/travel-newsletter-manager/-/commit/',
    'https://git.internetbrands.com/travel/flyertalk-wordpress/-/commit/',
]
# '/Users/tony/www/Davesgarden'

if argvs[1] == 'wdg':
    item_num = 0
elif argvs[1] == 'dg':
    item_num = 1
elif argvs[1] == 'gallery':
    item_num = 2
elif argvs[1] == 'wb':
    item_num = 3
elif argvs[1] == 'qb':
    item_num = 4
elif argvs[1] == 'fdr':
    item_num = 5
elif argvs[1] == 'gtt':
    item_num = 6
elif argvs[1] == 'fod':
    item_num = 7
elif argvs[1] == 'fodwp':
    item_num = 8
elif argvs[1] == 'tnm':
    item_num = 9
elif argvs[1] == 'gttwp':
    item_num = 10


branch_name = get_branch_name(target_pathes[item_num])
log_info = get_latest_git_log(target_pathes[item_num])
commit_id = get_latest_commit_id(target_pathes[item_num])

reviewers = [
    '[~mgutierrez]',
    '[~hgomez]',
    '*[~dathomas]*'
]

fdr_series_reviewers = [
    '[~dnavarro]'
];


random_thanks_words = [
    'Review it please, thanks.',
    'Actually, I am glad to see you have time to review it, thanks a lot.',
    'Please check it if you have free time, thanks.',
    'Check it please.'
    'Waiting for your code review, thank you.',
    'Review it if you have time,thanks a lot.'
]

git_commit_uri = git_commit_log_uris[item_num]

if item_num in (5, 6, 8, 9):
    # only have the one reviewer
    reviewers = fdr_series_reviewers
create_comment(reviewers, branch_name, log_info, commit_id, random_thanks_words, git_commit_uri)

